package com.drop.android.sdk;

import android.util.Log;

import com.drop.android.sdk.ad.VideoCallback;
import com.drop.android.sdk.ad.DropVideoIAP;
import com.drop.android.sdk.bridge.DropCocosBridge;
import com.drop.android.sdk.ad.DropVideoManger;
import com.drop.android.sdk.ad.DropMaxReward;

import org.json.JSONObject;

public class DropAdSdk {

    public static void showAd(String ad_source) {
        showAd(ad_source, "");
    }

    private static void adCallBack(String state, String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source", ad_source);
            DropCocosBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showAd(String ad_source, String q) {
        DropVideoIAP service = null;
        boolean isMaxReady = DropMaxReward.getInstance().isReady();
        if (isMaxReady) {
            service = DropVideoManger.getReward1Instance();
            Log.e("DROS", "show max reward");
        }

        if (service == null) {
            DropCocosBridge.getInstance().XSSdkCallback("ad_error_drop", "{}");
            return;
        }
        showAd(new VideoCallback() {
            @Override
            public void onAdOver() {
                adCallBack("ad_over_drop", ad_source);
            }

            @Override
            public void onAdError() {
                DropCocosBridge.getInstance().XSSdkCallback("ad_error_drop", "{}");
            }
        }, 0);
    }

    public static void showAd(VideoCallback callback, int i) {
        try {
            DropVideoManger.getReward1Instance().showVideo(DropCocosBridge.mainGameActivity.get(), new VideoCallback() {
                @Override
                public void onAdOver() {
                    if (callback != null) {
                        callback.onAdOver();
                    }
                }

                @Override
                public void onAdError() {
                    callbackAdError(callback);
                }
            });
        } catch (Exception e) {
            callbackAdError(callback);
        }
    }

    private static void callbackAdError(VideoCallback callback) {
        if (callback != null) {
            callback.onAdError();
        }
    }


}
